package com.cnfangmao.masterdata.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cnfangmao.masterdata.common.response.ResBody;
import com.cnfangmao.masterdata.common.response.ResCodeEnum;
import com.cnfangmao.masterdata.model.EnumValue;
import com.cnfangmao.masterdata.service.EnumValueService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 字段枚举值 前端控制器
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Api(value = "字段枚举值相关接口")
@RestController
@RequestMapping("/enumValue")
public class EnumValueController {
	
	@Autowired
	private EnumValueService enumValueService;

	/**
	 * 
	 * @param enumValueInfo
	 * @return
	 */
	@ApiOperation(value = "创建字段枚举值", produces = "application/json")
	@ApiImplicitParam(name = "enumValueInfo", value = "字段枚举值信息", required = true, dataType = "Json", 
						example = "{\"typeId\": \"typeId\", \"valueCode\": \"valueCode\", \"valueName\": \"valueName\"}")
	@PostMapping("/create.do")
	public ResBody create(String enumValueInfo) {
		ResBody resBody = null;
		try {
			EnumValue enumValue = JSONObject.parseObject(enumValueInfo, EnumValue.class);
			enumValue.setCreatedBy("user11");
			enumValue.setCreatedOn(new Date());
			enumValue.setModifiedBy("user11");
			enumValue.setModifiedOn(new Date());
			
			enumValueService.save(enumValue);

			resBody = ResBody.success();
		} catch (Exception e) {
			resBody = ResBody.failure().code(ResCodeEnum.PARAM_ERROR.getCode());
		}

		return resBody;
	}

	/**
	 * 
	 * @param enumValueInfo
	 * @return
	 */
	@ApiOperation(value = "更新字段枚举值", produces = "application/json")
	@ApiImplicitParam(name = "enumValueInfo", value = "字段枚举值信息", required = true, dataType = "Json", 
						example = "{\"valueId\":\"valueId\", \"typeId\": \"typeId\", \"valueCode\": \"valueCode\", \"valueName\": \"valueName\"}")
	@PostMapping("/update.do")
	public ResBody update(String enumValueInfo) {
		ResBody resBody = null;
		try {
			EnumValue enumValue = JSONObject.parseObject(enumValueInfo, EnumValue.class);
			enumValue.setModifiedBy("user11");
			enumValue.setModifiedOn(new Date());
			
			enumValueService.updateById(enumValue);

			resBody = ResBody.success();
		} catch (Exception e) {
			resBody = ResBody.failure().code(ResCodeEnum.PARAM_ERROR.getCode());
		}

		return resBody;
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "获取字段枚举值", produces = "application/json")
	@ApiImplicitParam(name = "id", value = "字段枚举值的ID", required = true, dataType = "String")
	@GetMapping("/getById.do")
	public ResBody getById(String id) {
		ResBody resBody = null;
		try {
			EnumValue enumValue = enumValueService.getById(id);

			resBody = ResBody.success(JSONObject.toJSON(enumValue));
		} catch (Exception e) {
			resBody = ResBody.failure().code(ResCodeEnum.PARAM_ERROR.getCode());
		}
		return resBody;
	}

	/**
	 * 
	 * @param queryCondition
	 * @return
	 */
	@ApiOperation(value = "获取字段枚举值列表", produces = "application/json")
	@ApiImplicitParam(name = "queryCondition", value = "查询条件(valueCode, valueName为like查询)", required = true, dataType = "Json", 
						example = "{\"valueId\":\"valueId\", \"typeId\":\"typeId\", \"valueCode\": \"valueCode\", \"valueName\": \"valueName\"}")
	@PostMapping("/list.do")
	public ResBody list(String queryCondition) {
		ResBody resBody = null;
		try {
			JSONObject jsonObj = JSONObject.parseObject(queryCondition);
			int currentPage = jsonObj.getIntValue("currentPage");
			int pageSize = jsonObj.getIntValue("pageSize");
			EnumValue enumValue = jsonObj.getObject("data", EnumValue.class);

			IPage<EnumValue> page = new Page<EnumValue>(currentPage, pageSize);

			QueryWrapper<EnumValue> queryWrapper = new QueryWrapper<EnumValue>(enumValue);
			IPage<EnumValue> enumValues = enumValueService.page(page, queryWrapper);

			resBody = ResBody.success(JSONObject.toJSON(enumValues));
		} catch (Exception e) {
			e.printStackTrace();
			resBody = ResBody.failure().code(ResCodeEnum.PARAM_ERROR.getCode());
		}
		return resBody;
	}

	/**
	 * 
	 * @return
	 */
	@ApiOperation(value = "获取所有字段枚举值列表", produces = "application/json")
	@GetMapping("/listAll.do")
	public ResBody listAll() {
		ResBody resBody = null;
		try {
			List<EnumValue> enumValues = enumValueService.list();

			resBody = ResBody.success(JSONObject.toJSON(enumValues));
		} catch (Exception e) {
			resBody = ResBody.failure().code(ResCodeEnum.PARAM_ERROR.getCode());
		}
		return resBody;
	}
	
}
